﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;

namespace Liebrand.UrlShortener
{
    public sealed class UrlShortenerFeatureReceiver : SPFeatureReceiver
    {

        #region Overrides

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebApplication oApp = (SPWebApplication)properties.Feature.Parent;

                oApp.WebConfigModifications.Add(CreateModification("add[@name='LiebrandUrlShortenerModule']",
                                                                   "configuration/system.web/httpModules",
                                                                   string.Format("<add name='LiebrandUrlShortenerModule' type='{0}' />", typeof(UrlShortenerHttpModule).AssemblyQualifiedName)));

                oApp.WebConfigModifications.Add(CreateModification("add[@name='LiebrandUrlShortenerModule']",
                                                   "configuration/system.webServer/modules",
                                                   string.Format("<add name='LiebrandUrlShortenerModule' type='{0}' />", typeof(UrlShortenerHttpModule).AssemblyQualifiedName)));

                oApp.Update();

                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

            }
            catch (Exception ex)
            {
                throw new SPException("Exception activating feature: " + ex);
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWebApplication oApp = (SPWebApplication)properties.Feature.Parent;

                List<SPWebConfigModification> toRemove = new List<SPWebConfigModification>();

                foreach (SPWebConfigModification m in oApp.WebConfigModifications)
                {
                    if (m.Owner == "Liebrand")
                        toRemove.Add(m);
                }

                foreach (SPWebConfigModification m in toRemove)
                    oApp.WebConfigModifications.Remove(m);

                oApp.Update();

                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error deactivating Url Shortener Feature");
                Trace.WriteLine(ex);
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
        #endregion

        #region Methods

        protected SPWebConfigModification CreateModification(string name, string path, string value)
        {
            var modification = new SPWebConfigModification(name, path);
            modification.Owner = "Liebrand";
            modification.Value = value;
            modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

            return modification;
        }
        #endregion
    }
}
